﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Browser;

namespace jLight
{
    public class jQueryObjectBase:  IEnumerable<jQueryObject>
    {
        public int Length
        {
            get
            {
                var d = (double)ScriptObject.GetProperty("length");
                return (int)d;
            }
        }


        public jQueryObjectBase(ScriptObject scriptObject)
        {
            this.ScriptObject = scriptObject;
        }

        public jQueryObjectBase()
        {
            //TODO: Change the version number to a constant
            this.ScriptObject = (ScriptObject)HtmlPage.Window.Eval("[{jLightVersion:0.1}][0]");
        }
        public ScriptObject ScriptObject { get; protected set; }

        public static jQueryObjectBase FromObject(object jObject)
        {
            if (jObject.GetType().IsGenericType)
            {
                jQueryObjectBase jqc = new jQueryObjectBase();
                jqc.ScriptObject = (ScriptObject)HtmlPage.Window.Eval(string.Format("[{0}][0]", jObject.ToString().Replace('=', ':')));
                return jqc;
            }
            if(jObject is ScriptObject)
            {
                jQueryObjectBase jqc = new jQueryObjectBase();
                jqc.ScriptObject = (ScriptObject)jObject;
                return jqc;
            }
            return null;
        }

        public static implicit operator jQueryObjectBase(Dictionary<string, object> properties)
        {
            jQueryObjectBase jqc = new jQueryObjectBase();
            jqc.SetProperties(properties);
            return jqc;
        }

        protected virtual void SetProperties(Dictionary<string, object> properties)
        {
        }

        protected virtual void SetJavascript()
        {
        }

        protected void InvokeScriptObject(string name, params object[] args)
        {
            InvokeScriptObject<object>(name, args);
        }

        protected T InvokeScriptObject<T>(string name, params object[] args)
        {
            if (!HtmlPage.IsEnabled || ScriptObject == null) return default(T);
            return (T)ScriptObject.Invoke(name, args);
        }

        public jQueryObject Invoke(string function, params object[] parameters)
        {
            return this.Invoke<ScriptObject>(function, parameters);
        }

        public T Invoke<T>(string function, params object[] parameters)
        {
            return (T)ScriptObject.Invoke(function, parameters);
        }

        public IEnumerator<jQueryObject> GetEnumerator()
        {
            List<jQueryObject> list = new List<jQueryObject>();
            for (int i = 0; i < this.Length; i++)
            {
                list.Add((ScriptObject)ScriptObject.GetProperty(i));
            }
            return list.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            List<jQueryObject> list = new List<jQueryObject>();
            for (int i = 0; i < this.Length; i++)
            {
                list.Add((ScriptObject)ScriptObject.GetProperty(i));
            }
            return list.GetEnumerator();
        }
    }
}